// 初始化
function initCart() {
    show(1, 15);
}

// 设置表格
function setModal(e) {
    var getData = igGetCart($(e).val());
    $('#user').val(getData.data.userName);
    $('#prodName').val(getData.data.prodName);
    $('#num').val(getData.data.num);
    $('#price').val(getData.data.price);
    $('#sum').val(getData.data.num * getData.data.price);
    $('#cartImg').attr('src', getData.data.img);
    $('#updateTime').val(getData.data.updateTime.replace('T', ' '));
    $('#createTime').val(getData.data.createTime.replace('T', ' '));
}


// 渲染分页查询
function show(pageNum, pageSize, phone) {
    var tbody = $('#tbody');
    var pageLink = $('#pageLink');
    if (!pageNum) {
        pageNum = pageLink.data('page').pageNum;
    }
    if (!pageSize) {
        pageSize = pageLink.data('page').pageSize;
    }

    var getData = pageCart(pageNum, pageSize, phone);

    if (pageNum > getData.data.pages && pageNum != 1) {
        show(getData.data.pages, pageSize, phone);
        return;
    }

    tbody.children().remove();
    var index = 1;
    getData.data.records.forEach(item => {
        const row = '<tr>\n' +
            '                            <th scope="row">' +
            '                               <input class="checkBox form-check-input" type="checkbox" id="check" name="option1" value="' + item.id + '">\n' +
            '                           </th>     ' +
            '                            <td><span class="d-flex justify-content-center"> ' + index + '</span></td>' +
            '                            <td><span class="d-flex justify-content-center">' + item.prodName + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.userName + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.price + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.num + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + (item.num * item.price) + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center"><img src="' + item.img + '"\n' +
            '                                     style="width: 50px; height: 35px;"></span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">\n' +
            '                                <span class="dropdown">\n' +
            '                                  <button class="btn dropdown-toggle" data-bs-boundary="viewport"\n' +
            '                                          data-bs-toggle="dropdown">操作</button>\n' +
            '                                    <ul class="dropdown-menu "\n' +
            '                                        style="min-width: 1px;">\n' +
            '                                        <li>\n' +
            '                                            <button  class="dropdown-item" data-bs-toggle="modal" data-bs-target="#itemModal" data-bs-type="edit" onclick="setModal(this)" value="' + item.id + '">详情</button>\n' +
            '                                        </li>\n' +
            '                                        <li>\n' +
            '                                            <button class="dropdown-item" onclick="deleteCart(this)" value="' + item.id + '">删除</button>\n' +
            '                                        </li>\n' +
            '                                    </ul>\n' +
            '                                </span>\n' +
            '                             </span></td>\n' +
            '                        </tr>';
        tbody.append(row);
        index++;
    });

    pageLink.data("page", {
        pages: getData.data.pages,
        pageNum: pageNum,
        pageSize: pageSize,
        phone: phone
    });
    // 设置选择头
    $('#checkHead').prop('checked', false);
    setPageFront( (pageNum - 1) * pageSize + 1,pageNum * pageSize > getData.data.total ? getData.data.total: pageNum * pageSize, getData.data.total);
    $('td').addClass('align-middle');
    loadPagingLabel(pageLink);
    setPagingLabel(pageLink);
    $('.page-item').on('click', function () {
        cartPageItem(this);
    });
}


// 处理分页的页数
function cartPageItem(e) {
    var targetPage = $(e).val();
    var pageLink = $('#pageLink');
    var getPageNum = pageLink.data("page").pageNum;
    var maxPage = pageLink.data("page").pages;
    var phone = pageLink.data("page").phone;
    if (targetPage > 0) {
        if (getPageNum == targetPage) {
            return;
        }
        show(targetPage, undefined, phone);
        return;
    }
    if (targetPage == 0) {
        targetPage = 1;
    }
    var finalPage = targetPage + getPageNum;
    if (finalPage < 1 || finalPage > maxPage) {
        return;
    }
    show(finalPage, undefined, phone);
}


